package com.blockchain.front.servlets.trading;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.front.service.trading.TradingManage;
import com.blockchain.util.parser.IntegerParser;

public class AddPj extends AbstractTradingServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		TradingManage tradingManage = serviceSession.getService(TradingManage.class);
		int scid = IntegerParser.parse(request.getParameter("scid"));
		String nr=request.getParameter("nr");
		int js=IntegerParser.parse(request.getParameter("js"));
		int yy=IntegerParser.parse(request.getParameter("yy"));
		int qj=IntegerParser.parse(request.getParameter("qj"));
		tradingManage.addPj(scid, nr, js, yy, qj);
		String url=getController().getViewURI(request,CommentMarket.class)+"?id="+scid;
		sendRedirect(request, response,url);
	}
	
	
	@Override
	protected void onThrowable(HttpServletRequest request,
			HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		getResourceProvider().log(throwable.getMessage());
		int scid = IntegerParser.parse(request.getParameter("scid"));
		String url=getController().getViewURI(request,CommentMarket.class)+"?id="+scid;
		if (throwable instanceof LogicalException
				|| throwable instanceof ParameterException) {
			prompt(request, response, PromptLevel.ERROR, throwable.getMessage());
			sendRedirect(request, response,url);
		}  else {
			super.onThrowable(request, response, throwable);
		}
	}
}
